var pathname = window.location.pathname;
var sessionPath = getCookie('application'); //sessionStorage.getItem("application");
//
//
//alert( " name = " + sessionPath + "\n" + "path = " + pathname)
//  if((sessionPath==""  && pathname != "/user_view/application_portal") ||
//  	(sessionPath==""  && pathname != "/" ) ||
//  	sessionPath==""  &&  pathname != "/login")){
// window.location.replace("/user_view/application_portal");
//  }

//if (sessionPath == "" && pathname != "/user_view/application_portal" && pathname != "/" && pathname != "/login") {
//    window.location.replace("/user_view/application_portal");
//}
if (sessionPath == "" && pathname != "/user_view/application_portal" && pathname != "/" && pathname != "/login" && pathname != "/user_view/workspace" && pathname != "/user_view/tour") {
    window.location.replace("/user_view/application_portal");
}
$(document).ready(function() {
	var pathname = window.location.pathname;
    if (pathname == "/user_view/organizational-chart") {
        $('.fl-user-navigation-wrapper').css('display', 'none');
        $('.fl-content').css('width', '100%');
        $('.fl-main-container').css('background', 'url(images/themes/workspace.jpg)');
    }
    ;
    if(pathname=="/"){
        $("[name='username']").focus();
    }
    function flcontentWandNavdisplay(){
        $('.fl-content').css('width', '100%');
        $('.fl-user-navigation-wrapper').css('display', 'none');
    }

    var urlPortal = '/user_view/application_portal';
    if (pathname == urlPortal) {
        flcontentWandNavdisplay();
        $('.fl-header-option').find('li:first').hide();
    }
    ;

    var urlRequestview = '/user_view/workspace';
    if (pathname == urlRequestview) {
        flcontentWandNavdisplay();
    }
    ;

    var urlNotifications = '/user_view/notifications';
    if (pathname == urlNotifications) {
    }
    ;

    var urlCreateOrganiaztionalChart = '/user_view/organizational_chart';
    if (pathname == urlCreateOrganiaztionalChart) {

         var breadCrumbs = $('ul.fl-breadcrums li a');
          breadCrumbs.removeClass('breadcrumbs');

         var matches = breadCrumbs.filter(function() {
            return document.location.href.indexOf($(this).attr('href')) >= 0;
          });

         matches.addClass('breadcrumbs');
         flcontentWandNavdisplay();

    }
    ;

    var urlCreateForms = '/user_view/formbuilder';
    if (pathname == urlCreateForms) {
        var breadCrumbs = $('ul.fl-breadcrums li a');
          breadCrumbs.removeClass('breadcrumbs');

         var matches = breadCrumbs.filter(function() {
            return document.location.href.indexOf($(this).attr('href')) >= 0;
          });

         matches.addClass('breadcrumbs');    
        flcontentWandNavdisplay();
    }
    ;

    var urlGeneratePrint = '/user_view/generate';
    if (pathname == urlGeneratePrint) {
       flcontentWandNavdisplay();
    }
    ;

     var urlCreateWorkflow = '/user_view/workflow';
    if (pathname == urlCreateWorkflow) {
        var breadCrumbs = $('ul.fl-breadcrums li a');
          breadCrumbs.removeClass('breadcrumbs');

         var matches = breadCrumbs.filter(function() {
            return document.location.href.indexOf($(this).attr('href')) >= 0;
          });

         matches.addClass('breadcrumbs');
       flcontentWandNavdisplay();
    }
    ;

     var urlCreateReports = '/user_view/report';
    if (pathname == urlCreateReports) {
       flcontentWandNavdisplay();
    }
    ;


    // For WOrkspace width configuration
    var user_view = $("#user_url_view").val();
    var path = window.location.pathname;
    if(path == user_view + "workspace"){
        $(".fl-content").css("width","100%");
  
    }

 

 /*   var menuItem = $('.mainmenu li a');
    menuItem.removeClass('current');

    var matches = menuItem.filter(function() {    
        //return document.location.href.indexOf($(this).attr('href')) >= 0;
        return pathname.indexOf($(this).attr('href')) >= 0;       
    });

    matches.addClass('current');*/


    $("body").hide();
    $("body").fadeIn("slow", "swing");


    $(".main-submenu").hide();

     $(".mainmenu li.fl-toggle-sub > a").click(function(event){
         event.preventDefault();
         $(this).parent().find('.main-submenu').slideToggle('fast');    
    });

    $(".fl-main-content-wrapper").delay(1000).animate({
        left: 0,
        opacity: 1
    }, "fast");

    $('.fl-user-avatar-wrapper').click(function() {
        $('.fl-header-option').slideToggle();
    });

     $('.fl-user-wrapper').on('click', function() {
        $('.fl-header-option').slideUp();
    });

     function me(eleToOpme){
        $(eleToOpme).find('.fl-delete').stop(false, false).animate({
            opacity: 1
        });
     }

     function ml(eleToOple){
        $(eleToOple).find('.fl-delete').stop(false, false).animate({
            opacity: 0
        });
     }

    $("body").on('mouseenter', ".fl-widget-starred-content-wrapper", function() {
        me(this);
    });

    $("body").on('mouseleave', ".fl-widget-starred-content-wrapper", function() {
        ml(this);
    });

    $('.fl-inbox-item').on('mouseenter', function() {
        $(this).find('.fl-delete').stop(false, false).animate({
            opacity: 1
        });
    });

    $(".fl-inbox-item").on('mouseleave', function() {
        $(this).find('.fl-delete').stop(false, false).animate({
            opacity: 0
        });
    });

    $('body').on('mouseenter', 'ul.notification li', function() {
        $(this).find('.fl-delete').stop(false, false).animate({
            opacity: 1
        });
    });

    $("body").on('mouseleave', "ul.notification li", function() {
        $(this).find('.fl-delete').stop(false, false).animate({
            opacity: 0
        });
    });

    $(".fl-module-icon li").on('mouseenter', function(){
        $(this).find('.fl-mod-title').addClass('fl-mod-title-hover')
    });

      $(".fl-module-icon li").on('mouseleave', function(){
        $(this).find('.fl-mod-title').removeClass('fl-mod-title-hover');
    });

    $(".fl-reply").click(function() {
        $(this).parents('.fl-message-body').eq(0).find('.fl-reply-box').fadeIn(500, function() {
            $(this).show();
        });

    });

    //$('#fl-create-btn').click(function(){
    //	$('#fl-send-msg-form').fadeIn(1000, function(){
    //		$(this).show();
    //		$(".crt_new_msg").show();
    //	});
    //	$('#fl-no-conversation-wrapper').hide();
    //});

    $("#fl-action-btn").click(function(e) {
        e.preventDefault();
        $("#fl-action-option").slideToggle('fast');
    });
    $('#tabs li a:not(:first)').addClass('inactive');
    $('.tab-container').hide();
    $('.tab-container:first').show();


    $('#tabs li a').click(function() {
        var t = $(this).attr('id');
        if ($(this).hasClass('inactive')) { //this is the start of our condition 
            $('#tabs li a').addClass('inactive');
            $(this).removeClass('inactive');

            $('.tab-container').hide();
            $('#' + t + 'C').fadeIn('slow');
        }
    });

    $('#fl-save_personalInfo').click(function() {
        $('.uInfo_211, .eInfo_211').toggle();
        $('#fl-cancel_personalInfo').toggle();

        $(this).val() == "Edit" ? Edit() : Save();

    });


    function Edit() {
        $('#fl-save_personalInfo').val("Save");
    }

    function Save() {
        $('#fl-save_personalInfo').val("Edit");
    }

    $('#fl-cancel_personalInfo').click(function() {
        $('.uInfo_211, .eInfo_211').toggle();
        $(this).hide();

        $('#fl-save_personalInfo').val() == "Edit" ? Edit() : Save();
    });

    $('#portal-app li').on('mouseenter', function() {
        $(this).find('.fl-app-btn').stop(false, false).animate({
            'margin-top': 50,
            'border-width': '20px',
            'border-color': '#f00'
        }, 'fast');
    });

    $('#portal-app li').on('mouseleave', function() {
        $(this).find('.fl-app-btn').stop(false, false).animate({
            'margin-top':180
        }, 'slow');
    });

    $('.fl-search-wrapper form').find('input[type="text"]').focusin(function() {
        $('input[type="button"]').fadeIn(400, function() {
            $(this).show();
        })
    });

    function toggleZoomScreen() {
        document.body.style.zoom = "50%"
    }

    $('#fl-showmenu').on('click', function() {
        $('.fl-user-navigation-wrapper').toggle('slide');

    });


    var flWdH = $('.fl-widget-wrapper').outerHeight();
    var flWCC = $('.fl-widget-wrapper-center-content').outerHeight();
    $('.fl-widget-wrapper-center-content').css({'margin-top': flWdH / 2 - flWCC / 2 - 23});

    $('.fl-header-option li:nth-child(2) a').on('mouseenter', function(){
        $(this).find('i').stop(false, false).animate({
            left:3,
            bottom:5
        });
    });

     $('.fl-header-option li:nth-child(2) a').on('mouseleave', function(){
        $(this).find('i').stop(false, false).animate({
            left:0,
            bottom:0
        });
    });


    $("body").on("click", ".setCategory", function() {
        var dataID = $(this).attr("data-id")
        var cookie_url = $('#cookie_url').val();
        // $.cookie("application", dataID);
        document.cookie = "application=" + dataID + ';domain=' + cookie_url + ';path=/';
        //sessionStorage.setItem("application",dataID);
    })

    $("body").on("click",".themeChange>li",function(){
        var theme = $(this).attr("data-theme");
        $.post("/ajax/theme",{theme:theme,action:"setTheme"},function(data){
            // alert(data)
            $(".user_css").attr("href","/css/user_css/stylesheets/"+theme+".css");
        })
    })

    if(pathname=="/user_view/application_portal"){
        $(".themeChangeLi").hide();
    }

    //workspace control hide show 
    $('.fl-form-properties-toggle').on('click', function(){
        $(this).attr('datas', true);
        if ($(this).attr('fl-form-prop') === undefined || $(this).attr('fl-form-prop') == 'true' ) {
              $(this).attr('fl-form-prop', false);
             $(this).closest('.fl-form-properties-wrapper').animate({
                    right:'-283'
                }, function(){
                    $(this).find('.fl-form-properties-toggle').find('i').removeClass('fa-chevron-right').addClass('fa-chevron-left');
             });

        }else {
            $(this).attr('fl-form-prop', true);
             $(this).closest('.fl-form-properties-wrapper').stop(false, false).animate({
                    right:0
                }, function(){
                    $(this).find('.fl-form-properties-toggle').find('i').removeClass('fa-chevron-left').addClass('fa-chevron-right');
            });
        }   
    });

    $('.fl-form-properties-wrapper .fl-widget-head:first').on('mouseenter', function(){
        $('.fl-form-properties-toggle').stop(false, false).animate({
            left:'-39'
        });
    });

    $('.fl-form-properties-wrapper .fl-widget-head').on('mouseleave', function(){
        $('.fl-form-properties-toggle').stop(false, false).animate({
            left:'-6'
        });
    });

    //slide up / slide down of form options
    $('.fl-min-max-properties').on('click', function(){
        var properties_dropdown = $(this).attr('fl-show-opt');
        //$(this).parents('fl-form-properties-wrapper').css('position', 'relative')
        $(this).parent().next().css('height', '132px');
        $(this).parent().next().slideToggle();
        if (properties_dropdown == 'true') {
             $(this).attr('fl-show-opt', false);
            $(this).find('i').removeClass('fa-minus').addClass('fa-plus');
           
        }else{
            $(this).attr('fl-show-opt', true);
            $(this).find('i').removeClass('fa-plus').addClass('fa-minus');

        }
    });

    //$('.fl-min-max-properties').click();

    $('.fl-min-max-properties2').on('click', function(){
        var properties_dropdown = $(this).attr('fl-show-opt');
        //$(this).parents('fl-form-properties-wrapper').css('position', 'relative')
        $(this).parent().next().css('height', '86px');
        $(this).parent().next().slideToggle();
        if (properties_dropdown == 'true') {
             $(this).attr('fl-show-opt', false);
            $(this).find('i').removeClass('fa-minus').addClass('fa-plus');
           
        }else{
            $(this).attr('fl-show-opt', true);
             $(this).find('i').removeClass('fa-plus').addClass('fa-minus');

        }
    });

    //$('.fl-min-max-properties2').click();

    $('.fl-props-container').children('div:last-child').css('margin-bottom', '10px');


    // Tour Guide
	$("body").on("click",".tour_guide_help",function(){
		var tour_name = $(this).attr("data-name");
		$("#wizard").stepy('destroy');
		$("#wizard-titles").remove();
		$("#wizard").html("");
		$(".tour_title_selected").html("");
		var self = $(this);
		self.find("img").show();
		//$('.wizard').stepy()
		//$("#takeATour").load("/json/take_tour/Sample.html");
		var ret = "";
		$.ajax({
			type: 'GET',       
			url: '/json/tour_guide.json',
			dataType: 'json',
			context: document.body,
			global: false,
			async:false,
			success: function(data) {
				var name = data['tour_guide'][tour_name];
				console.log(name)
				// Set title of the selected tour guide link
				var tour_title_selected = name['title'];
					$(".tour_title_selected").text(tour_title_selected.replace(/_/g," "));
				// Steps
				var tour_steps = name['steps'];
				
				for (var i = 0; i < tour_steps.length; i++) {
					ret += '<fieldset title="' + tour_steps[i]['title'] + '">';                            
						ret += '<legend>' + tour_steps[i]['subtitle'] + '</legend>';
						ret += '<div class="row-form">';
						   ret += '<table>';
						      ret += '<tr>';
							 ret += '<td class="tour_img">' + tour_steps[i]['images'] + '</td>';
							 ret += '<td style="vertical-align: baseline;">';
							 if (tour_steps[i]['data-title'] != "") {
								ret += '<p class="tour_title">' + tour_steps[i]['data-title'] + '</p>';
							 }
							    
							    var content = tour_steps[i]['data-content'];
							    //console.log(content)
							    //var ret_content = "";
							    
							    for(var i_content = 0; i_content < content.length; i_content++){
								//console.log(content[i_content])
								ret += '<p class="tour_content">'
								ret_content = content[i_content]['data'];
								//console.log(ret_content)
								ret += ret_content + "<br>";
								ret += '</p>';
							    }
							    
							 ret += '</td>';
						      ret += '</tr>';
						   ret += '</table>';
						ret += '</div>';                                          
					ret += '</fieldset>';
				}
				self.find("img").hide();
			    //return data;
			}	
		    });
		$("#wizard").html(ret);
		if($("#wizard").length > 0) $('#wizard').stepy();
		
//		.responseText
	});

    function flgoBack(){
        history.back();
    }

    if (history.length > 1) {
        $('.fl-backhistory').on('click', function(){
           flgoBack();
        });
    }else {
        $('.fl-backhistory').css('display', 'none');
    }

});



function getCookie(cname)
{
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++)
    {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0)
            return c.substring(name.length, c.length);
    }
    return "";
}
